import {a_soldier_actionproperty, a_soldier_actions} from "../commdata";
// import {Fighter} from "./fighter";
import g1 from "../sound/g1.wav";
import {Tank} from "./tank";
import {Basebio} from "./basebio";
import {ReleaseResource} from "../comm";
import {Obj_class} from "./obj_class";

export class A_soldier extends Basebio{

     imgyy:any;
     deadimg:any;


    constructor(_name:string,_live:number,_livelimit:number,_x:number,_y:number) {

        super();
        this.fun = "l"
        this.name=_name;
        this.live=_live;
        this.livelimit=_livelimit;
        this.demage = 5;
        this.x=_x;
        this.y=_y;
        this.currantactions=[];
        this.actionimg=new Image();
        this.imgyy=new Image();
        this.imgyy.src=a_soldier_actionproperty.yyimgsrc;
        this.deadimg=new Image();
        //this.deadimg.src= a_soldier_actionproperty.dead1;
        //this.sdadio.src=g1;
        this.ratewidth=a_soldier_actionproperty.ratewidth/2;
        this.rateheight=a_soldier_actionproperty.rateheight/2;
        this.actionindex = a_soldier_actionproperty.actionindex;
        this.actioncount = a_soldier_actionproperty.actioncount;
        this.actrons= a_soldier_actions;
        this.speed = 5;
        this.team = 0;
        this.fun="l";
    }

    actiondraw = (ptctx: any, max_width: number, max_height: number) => {

        if(this.isstart==false)
        {
            return;
        }

        if(this.live>0) {
            if (this.fun == "l") {
                this.x = this.x - this.speed;
            } else if (this.fun == "r") {
                this.x = this.x + this.speed;
            } else if (this.fun == "u") {
                this.y = this.y - this.speed;
            } else if (this.fun == "d") {
                this.y = this.y + this.speed;
            } else if (this.fun == "oul") {
                this.x = this.x - this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "our") {
                this.x = this.x + this.speed;
                this.y = this.y - this.speed;
            } else if (this.fun == "odl") {
                this.x = this.x - this.speed;
                this.y = this.y + this.speed;
            } else if (this.fun == "odr") {
                this.x = this.x + this.speed;
                this.y = this.y + this.speed;
            } else {
                this.x = this.x + this.speed;
            }

            if (this.x < 0) {
                this.actionindex = 0;
                this.fun = "r";
                this.x = 0;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.x > max_width - this.ratewidth) {
                this.actionindex = 0;
                this.fun = "l";
                this.x = max_width - this.ratewidth;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y < 0) {
                this.actionindex = 0;
                this.y = 0;
                this.fun = "d";
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else if (this.y > max_height - this.rateheight) {
                this.actionindex = 0;
                this.fun = "u";
                this.y = max_height - this.rateheight;
                this.currantactions = [];
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.fun == this.actrons[i].actiontype) {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            }
            this.currantactions = [];
            for (let i = 0; i < this.actrons.length; i++) {
                if (this.fun == this.actrons[i].actiontype) {
                    let temp = this.actrons[i];
                    this.currantactions.push(temp);
                }
            }
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
            ptctx?.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x, this.y, this.ratewidth, this.rateheight
            );
            if (this.actionindex < this.actioncount - 1)
                this.actionindex = this.actionindex + 1;
            else
                this.actionindex = 0;
        }
    }

    drawshowdo=(ptctx: any)=>{
        if(this.live>0) {
            //绘制阴影
            ptctx.globalAlpha = 0.4;
            ptctx.drawImage(this.imgyy, 0, 0, this.imgyy.width, this.imgyy.height, this.x, this.y + this.rateheight/2+8, this.ratewidth/2+5, this.rateheight/3);
            //ptctx.drawImage(this.imgyy, 0, 0, this.imgyy.width, this.imgyy.height, this.x + 5, this.y + this.rateheight - 15, this.ratewidth - 15, this.rateheight / 2 - 15);
            ptctx.globalAlpha = 1;
        }
    }

    deaddraw=(ptctx: any)=>{
        if(this.live<=0) {
            this.currantactions = [];
            //死亡
            if (this.fun == "l" || this.fun == "oul" || this.fun == "odl") {
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.actrons[i].actiontype== "dead_l") {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            } else {
                for (let i = 0; i < this.actrons.length; i++) {
                    if (this.actrons[i].actiontype == "dead_r") {
                        let temp = this.actrons[i];
                        this.currantactions.push(temp);
                    }
                }
            }
            this.actioncount=this.currantactions.length;
            this.actionimg.src = this.currantactions[this.actionindex].imgsrc;
            ptctx.drawImage(this.actionimg, 0, 0, this.actionimg.width, this.actionimg.height, this.x-20, this.y, this.actionimg.width/3, this.actionimg.height/2);
            if (this.actionindex < this.actioncount - 1) {
                this.actionindex = this.actionindex + 1;
            }
            else {
                this.actionindex = this.actioncount - 1;
                this.deadfinish = true;
                ReleaseResource(this);
            }

        }
    }

    drawinfo=(ptctx: any)=>{
        ptctx.font = "oblique small-caps bold 10px Arial";
        ptctx.fillStyle = "#CC3300";
        ptctx.fillText("编号："+this.name, this.x, this.y - 10);
        ptctx.fillText("生命：" + this.live.toString()+"/"+this.livelimit.toString(), this.x, this.y);
        ptctx.stroke();
    }

    setSoldierfire=(enemys:Obj_class[])=>{
        let flagnum: number = Math.floor(Math.random() * (100 - 0 + 1)) + 0;
        if(flagnum>55 && flagnum < 95)
        {
            this.isfire=true;
            for (let i = 0; i < enemys.length; i++) {
                let bisoobj:Basebio=enemys[i].objval;
                if(bisoobj.team!=this.team) {
                    //射中机率
                    let tmpflag = Math.floor(Math.random() * (70 - 0 + 1)) + 0;
                    if (tmpflag > 50) {

                        if (this.isAtacctRange(bisoobj) == true) {
                            //在攻击范围
                            if (bisoobj.live > 0)
                                bisoobj.live = bisoobj.live - this.demage;
                            else {
                                bisoobj.live = 0;
                            }
                            break;
                        }
                    }
                    // this.sdadio.play();
                    // fighter.live = fighter.live - this.demage;
                }
            }
        }
    }

    setaction=()=>{
        this.isfire=false;
        if(this.live>0) {
            let flagnum: number = Math.floor(Math.random() * (200 - 0 + 1)) + 0;
            if (flagnum > 10 && flagnum < 13) {
                this.actionindex = 0;
                this.fun = "u";
            } else if (flagnum > 30 && flagnum < 33) {
                this.actionindex = 0;
                this.fun = "d";
            } else if (flagnum > 55 && flagnum < 58) {
                this.actionindex = 0;
                this.fun = "l";
            } else if (flagnum > 70 && flagnum < 73) {
                this.actionindex = 0;
                this.fun = "r";
            } else if (flagnum > 100 && flagnum < 103) {
                this.actionindex = 0;
                this.fun = "oul";
            } else if (flagnum > 110 && flagnum < 113) {
                this.actionindex = 0;
                this.fun = "our";
            } else if (flagnum > 130 && flagnum < 133) {
                this.actionindex = 0;
                this.fun = "odr";
            } else if (flagnum > 180 && flagnum < 184) {
                this.actionindex = 0;
                this.fun = "odl";
            }

        }
    }
}